package com.wealth.system.web.filter;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.wealth.platform.entity.SldConfig;
import com.wealth.platform.enums.SldConfigTypeEnum;
import com.wealth.platform.service.ISldConfigService;
import com.wealth.platform.vo.SldConfigVO;

import iaas.redis.RedisService;

/**
 * 域名查询企业
 * @author chan
 * 
 * Changelog
 * --------------------------------
 * 2016-03-29 Set common attribute to session, such as corpid,path,resources 
 * 2016-09-13 Optimize JSON Tools net.sf.json to com.alibaba.fastjson
 */
@SuppressWarnings("all")
public class URLFilter implements Filter {
	
	private RedisService redisService;
	private ISldConfigService sldConfigService;
	
	/* ---------------- getter/setter ----------------*/

	@Override
	public void init(FilterConfig config) throws ServletException {
		ServletContext context = config.getServletContext();  
        ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);  
        redisService = (RedisService) ctx.getBean("redisService");  
        sldConfigService =  (ISldConfigService) ctx.getBean("sldConfigServiceImpl");
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest)req;   
        HttpServletResponse response = (HttpServletResponse) resp;  
        HttpSession session = request.getSession();
        
        try{
        	//过滤url
	        String url = request.getRequestURL().toString();
	        URL urlobj = new URL(url);
	        
		    if(url!=null && !url.equals("") && ( url.indexOf("resources")==-1 )) {   
		    	String domain = urlobj.getHost();
		    	
		    	//查询企业二级域名
		    	Long corpid = 2L;
		    	String path = "/taohuibang-wap";
		    	String resources = "/taohuibang-wap/resources";
		    	if(!redisService.exists("redis_sld_config")){
		    		List<SldConfigVO> sldConfgList = sldConfigService.getSldConfigVoList();
					String config = JSONArray.toJSONString(sldConfgList);
					redisService.set("redis_sld_config", config);
		    	}
		    	String config = redisService.get("redis_sld_config");
		    	
		    	//before array string ->jsonarray -> jsonobject ->bean
		    	//array string->List
		    	List<SldConfigVO> volist = JSON.parseArray(config, SldConfigVO.class);
		    	for (SldConfigVO vo : volist) {
		    		//compare sld && judge type
					if(vo.getDomain().contains(domain) && vo.getType()==SldConfigTypeEnum.WECHAT.getValue() ){					//比较二级域名
						corpid = vo.getCorpid();								//企业
						path = vo.getDomain();									//根目录
						resources = path+"/resources";					//资源目录
					}
				}
		    	
		    	//session设置企业，根目录，资源目录
		    	session.setAttribute("corpid", corpid);
		    	session.setAttribute("path", path);
		    	session.setAttribute("resources", resources);
	        }   
			chain.doFilter(request, response);
        } catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

}